package com.jbc.demoa.controller;

import com.jbc.demoa.common.ApiResponse;
import com.jbc.demoa.mapper.PatientStatusMapper;
import com.table.PatientStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Api(tags = "患者状态管理接口", description = "患者状态相关的操作接口")
@RestController
public class PatientStatusController {
    
    @Autowired
    private PatientStatusMapper patientStatusMapper;
    
    /**
     * 获取所有患者状态信息
     */
    @CrossOrigin
    @ApiOperation(value = "获取所有患者状态信息", 
                   notes = "获取系统中所有患者的状态信息，包括状态、科室、床位、姓名等")
    @RequestMapping(value = "/getAllPatientStatus", method = RequestMethod.GET)
    public ApiResponse<List<PatientStatus>> getAllPatientStatus() {
        try {
            List<PatientStatus> patientStatusList = patientStatusMapper.getAllPatientStatus();
            return ApiResponse.success("查询成功", patientStatusList);
        } catch (Exception e) {
            return ApiResponse.error("查询患者状态信息失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据状态获取患者信息
     */
    @CrossOrigin
    @ApiOperation(value = "根据状态获取患者信息", 
                   notes = "根据患者状态（如：危险、警告）获取相应的患者信息列表")
    @RequestMapping(value = "/getPatientStatusByStatus", method = RequestMethod.POST, consumes = "application/json; charset=UTF-8")
    public ApiResponse<List<PatientStatus>> getPatientStatusByStatus(
            @ApiParam(name = "jsonParamStr", value = "请求参数，JSON格式字符串，必须包含status字段", 
                      required = true, example = "{\"status\": \"危险\"}")
            @RequestBody String jsonParamStr) {
        try {
            com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(jsonParamStr);
            String status = jsonObject.getString("status");
            
            if (status == null || status.trim().isEmpty()) {
                return ApiResponse.error("状态不能为空");
            }
            
            List<PatientStatus> patientStatusList = patientStatusMapper.getPatientStatusByStatus(status);
            return ApiResponse.success("查询成功", patientStatusList);
        } catch (Exception e) {
            return ApiResponse.error("查询患者状态信息失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据科室获取患者信息
     */
    @CrossOrigin
    @ApiOperation(value = "根据科室获取患者信息", 
                   notes = "根据科室名称获取该科室的患者状态信息列表")
    @RequestMapping(value = "/getPatientStatusByDepartment", method = RequestMethod.POST, consumes = "application/json; charset=UTF-8")
    public ApiResponse<List<PatientStatus>> getPatientStatusByDepartment(
            @ApiParam(name = "jsonParamStr", value = "请求参数，JSON格式字符串，必须包含department字段", 
                      required = true, example = "{\"department\": \"心内科\"}")
            @RequestBody String jsonParamStr) {
        try {
            com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(jsonParamStr);
            String department = jsonObject.getString("department");
            
            if (department == null || department.trim().isEmpty()) {
                return ApiResponse.error("科室不能为空");
            }
            
            List<PatientStatus> patientStatusList = patientStatusMapper.getPatientStatusByDepartment(department);
            return ApiResponse.success("查询成功", patientStatusList);
        } catch (Exception e) {
            return ApiResponse.error("查询患者状态信息失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据医生手机号获取其管理的患者状态信息
     */
    @CrossOrigin
    @ApiOperation(value = "根据医生手机号获取其管理的患者状态信息", 
                   notes = "根据医生手机号获取该医生管理的所有患者的状态信息列表")
    @RequestMapping(value = "/getPatientStatusByDoctorPhone", method = RequestMethod.POST, consumes = "application/json; charset=UTF-8")
    public ApiResponse<List<PatientStatus>> getPatientStatusByDoctorPhone(
            @ApiParam(name = "jsonParamStr", value = "请求参数，JSON格式字符串，必须包含doctorPhone字段", 
                      required = true, example = "{\"doctorPhone\": \"13800138000\"}")
            @RequestBody String jsonParamStr) {
        try {
            com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(jsonParamStr);
            String doctorPhone = jsonObject.getString("doctorPhone");
            
            if (doctorPhone == null || doctorPhone.trim().isEmpty()) {
                return ApiResponse.error("医生手机号不能为空");
            }
            
            List<PatientStatus> patientStatusList = patientStatusMapper.getPatientStatusByDoctorPhone(doctorPhone);
            return ApiResponse.success("查询成功", patientStatusList);
        } catch (Exception e) {
            return ApiResponse.error("查询患者状态信息失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据医生手机号和状态获取患者信息
     */
    @CrossOrigin
    @ApiOperation(value = "根据医生手机号和状态获取患者信息", 
                   notes = "根据医生手机号和患者状态获取该医生管理的特定状态的患者信息列表")
    @RequestMapping(value = "/getPatientStatusByDoctorPhoneAndStatus", method = RequestMethod.POST, consumes = "application/json; charset=UTF-8")
    public ApiResponse<List<PatientStatus>> getPatientStatusByDoctorPhoneAndStatus(
            @ApiParam(name = "jsonParamStr", value = "请求参数，JSON格式字符串，必须包含doctorPhone和status字段", 
                      required = true, example = "{\"doctorPhone\": \"13800138000\", \"status\": \"危险\"}")
            @RequestBody String jsonParamStr) {
        try {
            com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(jsonParamStr);
            String doctorPhone = jsonObject.getString("doctorPhone");
            String status = jsonObject.getString("status");
            
            if (doctorPhone == null || doctorPhone.trim().isEmpty()) {
                return ApiResponse.error("医生手机号不能为空");
            }
            
            if (status == null || status.trim().isEmpty()) {
                return ApiResponse.error("状态不能为空");
            }
            
            List<PatientStatus> patientStatusList = patientStatusMapper.getPatientStatusByDoctorPhoneAndStatus(doctorPhone, status);
            return ApiResponse.success("查询成功", patientStatusList);
        } catch (Exception e) {
            return ApiResponse.error("查询患者状态信息失败: " + e.getMessage());
        }
    }
    
    /**
     * 添加患者状态信息
     */
    @CrossOrigin
    @ApiOperation(value = "添加患者状态信息", 
                   notes = "添加新的患者状态信息，包括状态、科室、床位、姓名等")
    @RequestMapping(value = "/addPatientStatus", method = RequestMethod.POST, consumes = "application/json; charset=UTF-8")
    public ApiResponse<String> addPatientStatus(
            @ApiParam(name = "jsonParamStr", value = "请求参数，JSON格式字符串，必须包含status、department、bedNumber、patientName字段", 
                      required = true, example = "{\"status\": \"危险\", \"department\": \"心内科\", \"bedNumber\": \"A101\", \"patientName\": \"张三\"}")
            @RequestBody String jsonParamStr) {
        try {
            com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(jsonParamStr);
            String status = jsonObject.getString("status");
            String department = jsonObject.getString("department");
            String bedNumber = jsonObject.getString("bedNumber");
            String patientName = jsonObject.getString("patientName");
            
            if (status == null || status.trim().isEmpty()) {
                return ApiResponse.error("状态不能为空");
            }
            
            if (department == null || department.trim().isEmpty()) {
                return ApiResponse.error("科室不能为空");
            }
            
            if (bedNumber == null || bedNumber.trim().isEmpty()) {
                return ApiResponse.error("床位号不能为空");
            }
            
            if (patientName == null || patientName.trim().isEmpty()) {
                return ApiResponse.error("患者姓名不能为空");
            }
            
            PatientStatus patientStatus = new PatientStatus(status, department, bedNumber, patientName);
            patientStatusMapper.insertPatientStatus(patientStatus);
            
            return ApiResponse.success("添加成功", "患者状态信息已添加");
        } catch (Exception e) {
            return ApiResponse.error("添加患者状态信息失败: " + e.getMessage());
        }
    }
    
    /**
     * 更新患者状态信息
     */
    @CrossOrigin
    @ApiOperation(value = "更新患者状态信息", 
                   notes = "根据ID更新患者状态信息")
    @RequestMapping(value = "/updatePatientStatus", method = RequestMethod.POST, consumes = "application/json; charset=UTF-8")
    public ApiResponse<String> updatePatientStatus(
            @ApiParam(name = "jsonParamStr", value = "请求参数，JSON格式字符串，必须包含id字段和其他需要更新的字段", 
                      required = true, example = "{\"id\": 1, \"status\": \"警告\"}")
            @RequestBody String jsonParamStr) {
        try {
            com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(jsonParamStr);
            Integer id = jsonObject.getInteger("id");
            
            if (id == null) {
                return ApiResponse.error("ID不能为空");
            }
            
            PatientStatus patientStatus = new PatientStatus();
            patientStatus.setId(id);
            
            if (jsonObject.getString("status") != null) {
                patientStatus.setStatus(jsonObject.getString("status"));
            }
            
            if (jsonObject.getString("department") != null) {
                patientStatus.setDepartment(jsonObject.getString("department"));
            }
            
            if (jsonObject.getString("bedNumber") != null) {
                patientStatus.setBedNumber(jsonObject.getString("bedNumber"));
            }
            
            if (jsonObject.getString("patientName") != null) {
                patientStatus.setPatientName(jsonObject.getString("patientName"));
            }
            
            patientStatus.setUpdateTime(LocalDateTime.now());
            patientStatusMapper.updatePatientStatus(patientStatus);
            
            return ApiResponse.success("更新成功", "患者状态信息已更新");
        } catch (Exception e) {
            return ApiResponse.error("更新患者状态信息失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据ID删除患者状态信息
     */
    @CrossOrigin
    @ApiOperation(value = "根据ID删除患者状态信息", 
                   notes = "根据ID删除指定的患者状态信息")
    @RequestMapping(value = "/deletePatientStatusById", method = RequestMethod.POST, consumes = "application/json; charset=UTF-8")
    public ApiResponse<String> deletePatientStatusById(
            @ApiParam(name = "jsonParamStr", value = "请求参数，JSON格式字符串，必须包含id字段", 
                      required = true, example = "{\"id\": 1}")
            @RequestBody String jsonParamStr) {
        try {
            com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(jsonParamStr);
            Integer id = jsonObject.getInteger("id");
            
            if (id == null) {
                return ApiResponse.error("ID不能为空");
            }
            
            patientStatusMapper.deletePatientStatusById(id);
            
            return ApiResponse.success("删除成功", "患者状态信息已删除");
        } catch (Exception e) {
            return ApiResponse.error("删除患者状态信息失败: " + e.getMessage());
        }
    }
}